package top.baselitch.sample.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import top.baselitch.sample.R;
import top.baselitch.sample.beans.User;

public class TableAdapter extends BaseAdapter{
    private LayoutInflater inflater;
    private List<User> users;
    private Map<Integer,Boolean> flgs;
    public TableAdapter(Context context, List<User> users) {
        inflater = LayoutInflater.from(context);
        this.users = users;
        initFlgs();
    }

    private void initFlgs(){
        flgs = new HashMap<>();
        for(int i=0;i<users.size();i++){
            flgs.put(i,false);
        }
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return users.size();
    }

    @Override
    public Object getItem(int position) {
        return users.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.item_table,null);
            viewHolder = new ViewHolder();
            viewHolder.userNameTv = (TextView) convertView.findViewById(R.id.item_userName);
            viewHolder.userEmailTv = (TextView) convertView.findViewById(R.id.item_userEmail);
            viewHolder.userPasswdTv = (TextView) convertView.findViewById(R.id.item_userPasswd);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        User user = users.get(position);
        viewHolder.userNameTv.setText(user.getUserName());
        viewHolder.userEmailTv.setText(user.getEmail());
        viewHolder.userPasswdTv.setText(user.getPasswd());
        if(flgs.get(position)){
            convertView.setBackgroundColor(0xFFEAEAEA);
        }else{
            convertView.setBackgroundColor(0xFFFFFFFF);
        }
        return convertView;
    }
    public void setFlg(int position,boolean flg){
        for(Integer integer : flgs.keySet()){
            if(integer==position){
                flgs.put(position,flg);
            }else{
                flgs.put(integer,false);
            }
        }
        notifyDataSetChanged();
    }

    public boolean getFlg(int position){
        return flgs.get(position);
    }
    public void clearFlg(){
        flgs.clear();
        for(int i=0;i<users.size();i++){
            flgs.put(i,false);
        }
    }

    public boolean isAllFlg(){
        for(Integer integer : flgs.keySet()){
            if(flgs.get(integer)){
                return true;
            }
        }
        return false;
    }
    private class ViewHolder{
        public TextView userNameTv,userEmailTv,userPasswdTv;
    }
}
